package cn.itsource.pethome.order.service.impl;

import cn.itsource.pethome.order.domain.OrderPetAcquisition;
import cn.itsource.pethome.order.domain.dto.OrderPetDTO;
import cn.itsource.pethome.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pethome.order.service.IOrderPetAcquisitionService;
import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.pet.domain.Pet;
import cn.itsource.pethome.pet.domain.SearchMasterMsg;
import cn.itsource.pethome.pet.mapper.PetMapper;
import cn.itsource.pethome.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-27
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    @Transactional
    public void confirm(OrderPetDTO dto) {
        //订单的修改
        OrderPetAcquisition order = orderPetAcquisitionMapper.queryOne(dto.getId());
        //设置为2就是完成
        order.setState(2);
        order.setPrice(dto.getMoney());
        order.setPaytype(dto.getPayType());
        orderPetAcquisitionMapper.update(order);
        //pet的修改
        Pet pet = petMapper.queryOne(order.getPetId());
        pet.setCostprice(dto.getMoney().toString());
        petMapper.update(pet);
        //寻主表
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryOne(order.getSearchMasterMsgId());
        searchMasterMsg.setState(5);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    @Override
    public void reject(OrderPetDTO dto) {
        //订单的修改
        OrderPetAcquisition order = orderPetAcquisitionMapper.queryOne(dto.getId());
        //设置为3就是取消
        order.setState(3);
        orderPetAcquisitionMapper.update(order);
        //pet的修改
        petMapper.delete(order.getPetId());
        //寻主表
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryOne(order.getSearchMasterMsgId());
        //放入寻主池
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
    }
}
